/*
 * @Author: xiaosihan
 * @Date: 2024-01-10 10:43:35
 * @Last Modified by: xiehangyun
 * @Last Modified time: 2025-05-16 22:38:28
 */

// 全部图库的公共状态
import get from 'lodash/get';
import { configure, observable } from 'mobx';
import materialsApi from './materialsApi';

configure({ enforceActions: 'never' });

// 全部图库管理状态
const materialStore = observable({

    //是否显示弹窗
    visible: false,
    setVisible(visible) {
        this.visible = visible;
    },

    // 全部组
    cascaderOptions: [],

    async getStickerGroup() {
        // const result = (await materialsApi.get_sticker_group().catch(() => []));
        // let cascaderOptions = result.map((g) => {
        //     return {
        //         value: g.id,
        //         text: g.name,
        //         children: (g.sonList || []).map((g) => {
        //             return {
        //                 value: g.id,
        //                 text: g.name,
        //             };
        //         }),
        //     };
        // });

        // this.cascaderOptions = cascaderOptions;
        // this.setActiveid([
        //     get(cascaderOptions, [0, 'value']),
        //     get(cascaderOptions, [0, 'children', 0, 'value']),
        // ]);
    },

    // 当前选中的分组
    activeid: [0, 0],
    async setActiveid(activeid) {
        this.activeid = activeid;
        this.page = 1;
        this.hasNext = true;
        this.stickers = [];
        this.loading = true;
        await new Promise(resolve => setTimeout(resolve, 100));
        this.getSticker();
    },

    // 加载中
    loading: false,
    setLoading(loading) {
        if (loading) {
            this.loading = loading;
        } else {
            setTimeout(() => {
                this.loading = loading;
            }, 300);
        }
    },
    page: 1,
    getNextPage() {
        if (this.loading || !this.hasNext) {
            return;
        }
        this.page += 1;
        this.getSticker();
    },
    pageSize: 10,
    hasNext: true,
    // 搜索词
    searchValue: '',
    setSearchValue(searchValue) {
        this.page = 1;
        this.stickers = [];
        this.searchValue = searchValue;
        this.hasNext = true;
        this.getSticker();
    },
    // 图库列表
    stickers: [],
    async getSticker() {
        // this.setLoading(true);
        // const { page, pageSize, searchValue, activeid } = this;
        // const { list, totalCount } = await materialsApi
        //     .get_sticker({ groupId: activeid[1], pageNo: page, pageSize, searchValue })
        //     .catch(() => []);
        // this.stickers.push(...list);
        // this.hasNext = list.length === this.pageSize;
        // this.setLoading(false);
    },
});

// 初始获取图库组
materialStore.getStickerGroup();

export default materialStore;
